using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Client
{
    public partial class VideoSettings : Form
    {
        public VideoSettings()
        {
            InitializeComponent();

            // Set focus on play button
            btnPlay.Select();
            loadFile();
        }

        public bool setSetting(string name, string value)
        {
            if (name.StartsWith("name"))
            {
                txfName.Text = value;
            }

            else if (name.StartsWith("resolution"))
            {
                string[] s = value.Split('x');
                comboBox1.Text = s[0].Trim();
                comboBox2.Text = s[1].Trim();
            }

            else if (name.StartsWith("fullscreen"))
            {
                comboBox3.Text = value;
            }

            else if (name.StartsWith("ip_adress"))
            {
                txfIp.Text = value;
            }

            return false;
        }

        public void loadFile()
        {
            try
            {
                FileStream file = new FileStream("data/config/settings.cfg", FileMode.Open, FileAccess.Read);
                StreamReader sr = new StreamReader(file);
                string line = "";
                while ((line = sr.ReadLine()) != null)
                {
                    string[] s = line.Split('=');
                    if (!s[0].StartsWith("#"))
                    {
                        if(s.Length > 1)
                            setSetting(s[0].Trim(), s[1].Trim());
                    }
                }
                sr.Close();
                sr.Dispose();
                file.Close();
                file.Dispose();
            }

            catch (FileNotFoundException)
            {
                saveFile();
            }
        }

        public void saveFile()
        {
            FileStream file = new FileStream("data/config/settings.cfg", FileMode.Create, FileAccess.Write);
            StreamWriter sw = new StreamWriter(file);
            sw.WriteLine("####################################################");
            sw.WriteLine("# autogenerated config file for settings of our game");
            sw.WriteLine("####################################################");
            sw.WriteLine();
            sw.WriteLine("# player name");
            sw.WriteLine("name = {0}", GetName());
            sw.WriteLine();
            sw.WriteLine("####################################################");
            sw.WriteLine("# Graphics config");
            sw.WriteLine("####################################################");
            sw.WriteLine("# resolution");
            sw.WriteLine("resolution = {0}x{1}", GetWidth(), GetHeight());
            sw.WriteLine("# fullscreen");
            sw.WriteLine("fullscreen = {0}", GetFullscreen());
            sw.WriteLine();
            sw.WriteLine("####################################################");
            sw.WriteLine("# connection config");
            sw.WriteLine("####################################################");
            sw.WriteLine("# ip adress");
            sw.WriteLine("ip_adress = {0}", GetIp());
            sw.Flush();
            sw.Close();
            file.Close();
            file.Dispose();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            saveFile();
            this.Close();
        }

        public int GetWidth()
        {
            try { return Int32.Parse(comboBox1.Text); }
            catch (Exception) { return 800; }
        }

        public int GetHeight()
        {
            try { return Int32.Parse(comboBox2.Text); }
            catch (Exception) { return 600; }
        }

        public bool GetFullscreen()
        {
            try { return Boolean.Parse(comboBox3.Text); }
            catch (Exception) { return false; }
        }

        public string GetIp()
        {
            if (txfIp.Text != "") { return txfIp.Text; }
            return "localhost";
        }

        public int GetPort()
        {
            return 9997;
        }

        public string GetName()
        {
            if (txfName.Text != "") { return txfName.Text; }
            return "piet paulusma";
        }

        private void VideoSettings_FormClosed(object sender, FormClosedEventArgs e)
        {
            
        }
    }
}